package com.zjut.an.role.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.criterion.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zjut.an.news.dao.AnNews;

/**
 * A data access object (DAO) providing persistence and search support for
 * AnRole entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.zjut.an.role.dao.AnRole
 * @author MyEclipse Persistence Tools
 */

public class AnRoleDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(AnRoleDAO.class);
	// property constants
	public static final String RO_ID = "roId";
	public static final String RO_NAME = "roName";
	public static final String RO_WEBCLASS = "roWebclass";
	public static final String NEWSPOWER = "newspower";
	public static final String CHECKPOWER = "checkpower";
	public static final String CLASSPOWER = "classpower";
	public static final String USERPOWER = "userpower";
	public static final String ADMINPOWER = "adminpower";
	public static final String SYSTEMPOWER = "systempower";

	protected void initDao() {
		// do nothing
	}

	public void sort(Criteria criteria, String sort, String order) {
		if (sort!=null && order!=null) {
			System.out.println(sort + order + " sort !!!!!!!!!!!!!!!!");
			if (order.equals("asc")) {
				criteria.addOrder(Order.asc(sort));
			} else {
				criteria.addOrder(Order.desc(sort));
			}
		} else {
			criteria.addOrder(Order.desc(RO_ID));			
		}
	}

	public void save(AnRole transientInstance) {
		log.debug("saving AnRole instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void update(AnRole transientInstance) {
		log.debug("updating AnRole instance");
		try {
			getHibernateTemplate().update(transientInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	public void delete(AnRole persistentInstance) {
		log.debug("deleting AnRole instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public AnRole findById(java.lang.Integer id) {
		log.debug("getting AnRole instance with id: " + id);
		try {
			AnRole instance = (AnRole) getHibernateTemplate().get(
					"com.zjut.an.role.dao.AnRole", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(AnRole instance) {
		log.debug("finding AnRole instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding AnRole instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from AnRole as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByRoName(Object roName) {
		return findByProperty(RO_NAME, roName);
	}

	public List findByRoWebclass(Object roWebclass) {
		return findByProperty(RO_WEBCLASS, roWebclass);
	}

	public List findByNewspower(Object newspower) {
		return findByProperty(NEWSPOWER, newspower);
	}

	public List findByCheckpower(Object checkpower) {
		return findByProperty(CHECKPOWER, checkpower);
	}

	public List findByClasspower(Object classpower) {
		return findByProperty(CLASSPOWER, classpower);
	}

	public List findByUserpower(Object userpower) {
		return findByProperty(USERPOWER, userpower);
	}

	public List findByAdminpower(Object adminpower) {
		return findByProperty(ADMINPOWER, adminpower);
	}

	public List findBySystempower(Object systempower) {
		return findByProperty(SYSTEMPOWER, systempower);
	}

	public List findAll() {
		log.debug("finding all AnRole instances");
		try {
			String queryString = "from AnRole";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public AnRole merge(AnRole detachedInstance) {
		log.debug("merging AnRole instance");
		try {
			AnRole result = (AnRole) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(AnRole instance) {
		log.debug("attaching dirty AnRole instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(AnRole instance) {
		log.debug("attaching clean AnRole instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static AnRoleDAO getFromApplicationContext(ApplicationContext ctx) {
		return (AnRoleDAO) ctx.getBean("AnRoleDAO");
	}
}